﻿using BuyWithoutLoseMoney.Application.User.Dots;
using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;

namespace DotNetCoreDemo.Application.Service
{
    public class UserService : IUserService
    {
        private readonly IRepository<User, int> _UserRepository;
        public UserService(IRepository<User, int> userRepository)
        {
                _UserRepository = userRepository  ;
        }
        public async Task<PagedResultDto<User>> GetPaginatedResult(UserInput userInput)
        {
            var query = _UserRepository.GetAll().Where(x => 1 == 1);
            if (!string.IsNullOrEmpty(userInput.NickName))
            {
                query = query.Where(x => x.NickName.Contains(userInput.NickName));
            }
            //条件
            var count = query.Count();
            var list = await query.Take(userInput.Limit).Skip((userInput.Page - 1) * userInput.Limit).AsNoTracking().ToListAsync();

            return new PagedResultDto<User>
            {
                Data = list,
                TotalCount = count,
                Page = userInput.Page,
                Limit = userInput.Limit,
            };
            
        }
    }
}
